﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MusicAcademy
{
    public partial class frmPhoneBook : Form
    {
        public int recordID = -1;
        private int ContactDetailID = -1;

        public frmPhoneBook()
        {
            InitializeComponent();
        }

        private void RefreshGrid()
        {
            fullContactsTableAdapter.Fill(musicAcademyDataSet.FullContacts, txtFirstNameSearch.Text, txtLastNameSearch.Text, txtValueSearch.Text);
        }

        private bool isValid()
        {
            bool valid = true;
            if (txtFirstName.Text.Trim() == "")
                valid = Globals.SetError(txtFirstName);
            else
                Globals.SetValid(txtFirstName);
            
            if (txtLastName.Text.Trim() == "")
                valid = Globals.SetError(txtLastName);
            else
                Globals.SetValid(txtLastName);
                        
            return valid;
        }

        private bool isValidDetail()
        {
            bool valid = true;
            if (txtValue.Text.Trim() == "")
                valid = Globals.SetError(txtValue);
            else
                Globals.SetValid(txtValue);

            return valid;
        }
        private void frmPhoneBook_Load(object sender, EventArgs e)
        {
             if (UserManager.HasAccess(this.Name, "View") == false)
            {
                MessageBox.Show("شما اجازه ی دیدن این فرم را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Close();
            }
            // TODO: This line of code loads data into the 'musicAcademyDataSet.ContactType' table. You can move, or remove it, as needed.
            this.contactTypeTableAdapter.Fill(this.musicAcademyDataSet.ContactType);

            RefreshGrid();
            grdContacts.ClearSelection();
        }

        private void txtFirstNameSearch_TextChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void txtLastNameSearch_TextChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void txtValueSearch_TextChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void grdContacts_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            txtFirstName.Text = grdContacts.Rows[e.RowIndex].Cells["firstName"].Value.ToString();
            txtLastName.Text = grdContacts.Rows[e.RowIndex].Cells["lastName"].Value.ToString();
            recordID = int.Parse((grdContacts.Rows[e.RowIndex].Cells["ID"].Value.ToString()));
            fullContactDetailTableAdapter.Fill(musicAcademyDataSet.FullContactDetail, recordID);
            grdDetail.ClearSelection();
            btnDelete.Enabled = true;
            txtValue.Enabled = true;
            cmbType.Enabled = true;
            btnSaveDetail.Enabled = true;
            btnCancelDetail.Enabled = true;
            Globals.SetValid(txtFirstName);
            Globals.SetValid(txtLastName);
            Globals.SetValid(txtValue);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            PhoneBook p = new PhoneBook();
            if (isValid() == false)
                return;
            if (recordID == -1)
            {
                if (UserManager.HasAccess(this.Name, "Insert") == false)
                {
                    MessageBox.Show("شما اجازه ی اضافه کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                recordID = p.AddContact(txtFirstName.Text, txtLastName.Text);
                RefreshGrid();
                btnDelete.Enabled = true;
            }
            else
            {
                if (UserManager.HasAccess(this.Name, "Update") == false)
                {
                    MessageBox.Show("شما اجازه ی ویرایش کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                p.EditContact(recordID, txtFirstName.Text, txtLastName.Text);
                RefreshGrid();
            }
            MessageBox.Show("ذخیره شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void grdDetail_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            txtValue.Text = grdDetail.Rows[e.RowIndex].Cells["value"].Value.ToString();
            cmbType.SelectedValue= grdDetail.Rows[e.RowIndex].Cells["contactTypeID"].Value;
            ContactDetailID = int.Parse((grdDetail.Rows[e.RowIndex].Cells["_ID"].Value.ToString()));
            btnDeleteDetail.Enabled = true;
            btnSaveDetail.Enabled = true;
            btnCancelDetail.Enabled = true;
            txtValue.Enabled = true;
            cmbType.Enabled = true;
            Globals.SetValid(txtValue);
        }

        private void btnCancelDetail_Click(object sender, EventArgs e)
        {
            txtValue.Text = "";
            cmbType.SelectedIndex = 0;
            ContactDetailID = -1;
            Globals.SetValid(txtValue);
            btnDeleteDetail.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnDeleteDetail.Enabled = false;
            btnSaveDetail.Enabled = false;
            btnCancelDetail.Enabled = false;
            txtFirstName.Text = "";
            txtLastName.Text = "";
            recordID = -1;
            ContactDetailID = -1;
            txtValue.Text = "";
            cmbType.SelectedIndex = 0;
            txtValue.Enabled = false;
            cmbType.Enabled = false;
            fullContactDetailTableAdapter.Fill(musicAcademyDataSet.FullContactDetail, recordID);
            btnDelete.Enabled = false;
            Globals.SetValid(txtFirstName);
            Globals.SetValid(txtLastName);
            Globals.SetValid(txtValue);
        }

        private void btnSaveDetail_Click(object sender, EventArgs e)
        {
            PhoneBook p = new PhoneBook();
            if (isValidDetail() == false)
                return;
            if (ContactDetailID == -1)
            {
                if (UserManager.HasAccess(this.Name, "Insert") == false)
                {
                    MessageBox.Show("شما اجازه ی اضافه کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                ContactDetailID = p.AddDetail(recordID, (int)cmbType.SelectedValue, txtValue.Text);
                btnDeleteDetail.Enabled = true;
            }
            else
            {
                if (UserManager.HasAccess(this.Name, "Update") == false)
                {
                    MessageBox.Show("شما اجازه ی ویرایش کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                ContactDetailID = p.UpdateDetail(ContactDetailID, txtValue.Text);
            }
            fullContactDetailTableAdapter.Fill(musicAcademyDataSet.FullContactDetail, recordID);
            RefreshGrid();
        }

        private void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if (UserManager.HasAccess(this.Name, "Delete") == false)
            {
                MessageBox.Show("شما اجازه ی حذف کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (MessageBox.Show("آیا از حذف این رکورد اطمینان دارید ؟", "حذف", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                PhoneBook p = new PhoneBook();
                p.UpdateDetail(ContactDetailID, "");
                p.CleanUpContact(recordID);
                fullContactDetailTableAdapter.Fill(musicAcademyDataSet.FullContactDetail, recordID);
                RefreshGrid();
                Globals.SetValid(txtValue);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (UserManager.HasAccess(this.Name, "Delete") == false)
            {
                MessageBox.Show("شما اجازه ی حذف کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (MessageBox.Show("آیا از حذف این رکورد اطمینان دارید ؟", "حذف", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                SqlConnection con = new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "DELETE FROM ContactDetail WHERE contactID=@contactID; DELETE FROM Contact WHERE ID=@contactID";
                com.Parameters.AddWithValue("@contactID", recordID);

                con.Open();
                com.ExecuteNonQuery();
                con.Close();

                btnDeleteDetail.Enabled = false;
                btnSaveDetail.Enabled = false;
                btnCancelDetail.Enabled = false;
                txtFirstName.Text = "";
                txtLastName.Text = "";
                recordID = -1;
                ContactDetailID = -1;
                txtValue.Text = "";
                cmbType.SelectedIndex = 0;
                txtValue.Enabled = false;
                cmbType.Enabled = false;
                fullContactDetailTableAdapter.Fill(musicAcademyDataSet.FullContactDetail, recordID);
                RefreshGrid();
                btnDelete.Enabled = false;

                Globals.SetValid(txtFirstName);
                Globals.SetValid(txtLastName);
                Globals.SetValid(txtValue);
                MessageBox.Show("حذف شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
